package com.example.chat.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONPath;
import com.example.chat.service.HttpService;
import com.example.common.common.config.filter.UserContext;
import com.example.common.common.enums.HttpEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.example.common.utils.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2025/8/10 14:45
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class HttpServiceImpl implements HttpService {
    @Override
    public void processDocuments(String fileId) {
        Map<String, Object> payload = new HashMap<>();
        payload.put("fileKeys", List.of(StringUtils.replaceStr(fileId)));
        payload.put("userId", StringUtils.replaceStr(UserContext.getCurrentUser().getUserId()));

        String response = HttpUtil.post(HttpEnum.ANALYSIS_FILE.getUrl(), JSONUtil.toJsonStr(payload));
        log.info("vector file :{}", response);
        JSONArray messages = (JSONArray) JSONPath.eval(response, "$.results[?(@.status == 'error')].message");
        if (messages != null) {
            throw new RuntimeException(String.valueOf(messages));
        }
    }
}
